//
//  SCCookie.swift
//  SwiftCommon
//
//  Created by lijl on 15/7/23.
//  Copyright (c) 2015年 lijialong. All rights reserved.
//


import Foundation

/// 访问Cookie的相关方法
public class SCCookie{
    
    
    /// 获取存放Cookie的Storage
    public class func GetCookieStorage()->NSHTTPCookieStorage{
        return NSHTTPCookieStorage.sharedHTTPCookieStorage()
    }
    
    /// 获取所有Cookie数组
    public class func GetCookieArray()->[NSHTTPCookie]{
        
        let cookieStorage = GetCookieStorage()
        let cookieArray = cookieStorage.cookies
        if let arr = cookieArray{
            return cookieArray as! [NSHTTPCookie]
        }
        else{
            return []
        }
    }
    
    /// 获取Cookie值
    public class func GetCookieByName(let cookieName:String)->String?
    {
        let cookieArray:[NSHTTPCookie] = GetCookieArray()
        var value:String?
        if cookieArray.count > 0
        {
            
            for cookie in cookieArray
            {
                
                if cookie.name == cookieName
                {
                    value = cookie.value
                    break
                }
            }
        }
        return value
    }
    
    /// 根据Cookie内容数据，获取一个对应的NSDictionary数据
    public class func GetRequestFiledByCookie(let cookieAttay:[NSHTTPCookie])->NSDictionary{
        
        let requestFiled = NSHTTPCookie.requestHeaderFieldsWithCookies(cookieAttay)
        return requestFiled as NSDictionary
    }
}